package cn.jingzhuan.fm.ui.activity

import android.os.Bundle
import android.support.v7.widget.LinearLayoutCompat
import android.support.v7.widget.LinearLayoutManager
import cn.jingzhuan.fm.R
import cn.jingzhuan.fm.databinding.ActivityFmIndexBinding
import cn.jingzhuan.fm.mvp.contract.FMIndexContract
import cn.jingzhuan.fm.mvp.presenter.FMIndexPresenter
import cn.jingzhuan.fm.pojo.ADBanner
import cn.jingzhuan.fm.pojo.FMAlbum
import cn.jingzhuan.fm.pojo.AlbumTodayListen
import cn.jingzhuan.fm.ui.epoxy.controller.FMIndexAdapterController
import javax.inject.Inject

class FMIndexActivity : BaseBindingActivity<ActivityFmIndexBinding>() , FMIndexContract.View{

    lateinit var controller: FMIndexAdapterController

    @Inject lateinit var fmIndexPresenter: FMIndexPresenter

    override fun onDataBind(savedInstanceState: Bundle?, binding: ActivityFmIndexBinding) {
        controller = FMIndexAdapterController(this.baseContext)
        setUpActionBar(binding.viewToolbar?.toolbar)
        binding.viewToolbar?.title = resources.getString(R.string.app_name)

        val layoutManager = LinearLayoutManager(baseContext, LinearLayoutCompat.VERTICAL, false)
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.adapter = controller.adapter

        // 刷新事件
        binding.swipeRefreshLayout.setOnRefreshListener {
            initData()
            refreshFinish()
        }

        initData()
    }

    override fun getLayoutId(): Int = R.layout.activity_fm_index


    // 数据初始化
    fun initData() {
        fmIndexPresenter.getBannerList()
        fmIndexPresenter.getTodayListenList()
        fmIndexPresenter.getAlbumRecommendList()
    }


    override fun showBannerList(bannerList: List<ADBanner>) {
        controller.setBannerList(bannerList)
    }

    override fun showTodayListenList(todayListenList: List<AlbumTodayListen>) {
        controller.setTodayListenList(todayListenList)
    }

    override fun showAlbumRecommendList(albumRecommendList: List<FMAlbum>) {
        controller.setAlbumRecommendList(albumRecommendList)
    }

    override fun refreshFinish() {
        runOnUiThread {
            binding.swipeRefreshLayout.isRefreshing = false
        }
    }

}